<?php
namespace app\index\controller;
use app\index\model\PoliceStationModel;
use app\index\model\UserPoliceStationModel;
use think\Validate;
use app\common\controller\BaseController;
use app\index\model\AppUserModel;

class AppUser extends BaseController{

    public function app_user(){
        return $this->authAjaxReturn(200,null,"success");
    }

    //查询
    public function admin_query(){
        $input_query = input("get.input_query",'');
        $page = input("get.page",1);
        $limit = input("get.limit",8);
        $model = new AppUserModel();

        $map['account'] = ["like",'%'.$input_query.'%'];
        $map['nick_name'] = ["like",'%'.$input_query.'%'];
        $map['phone'] = ["like",'%'.$input_query.'%'];
        $data = $model
            ->alias('a')
            ->order('a.create_time desc')
            ->whereOr($map)
            ->page($page,$limit)
            ->select();
        if ($data){
            return $this->ajaxReturn(200,$data,'存在你查找的信息');
        }else{
            return $this->ajaxReturn(400,$data,'不存在你查找的信息');
        }

    }

    public function room_query(){
        $model = new AppUserModel();
        $page = input("get.page",1);
        $limit = input("get.limit",8);

        $data = $model
            ->alias('a')
            ->order('a.create_time desc')
            ->where('type','=',1)
            ->page($page,$limit)
            ->select();
        if ($data){
            return $this->ajaxReturn(200,$data,'存在你查找的信息');
        }else{
            return $this->ajaxReturn(400,$data,'不存在你查找的信息');
        }

    }

    public function getTypePageLimit(){
        $model = new AppUserModel();
        $data = $model->where('type','=',1)->count();
        return $this->ajaxReturn(200,$data,'刷新成功');
    }

    //获取用户信息总数
    public function getPageLimit(){
        $model = new AppUserModel();
        $data = $model->count();
        return $this->ajaxReturn(200,$data,'刷新成功');
    }

    //获取条件查询用户信息总数路由
    public function getQueryPageLimit(){
        $input_query = input("get.input_query");
        $model = new AppUserModel();
        $map['account'] = ["like",'%'.$input_query.'%'];
        $map['nick_name'] = ["like",'%'.$input_query.'%'];
        $map['phone'] = ["like",'%'.$input_query.'%'];
        $data = $model->whereOr($map)->count();
        return $this->ajaxReturn(200,$data,'获取成功');
    }


    //添加用户
    public function add(){

        $account = input("post.account");
        $sex = input("post.sex");
        $type = input("post.type");
        $password = input("post.password");
        $police_station_id = input('post.police_station_id');
        $phone = input("post.phone");
        $nick_name = input("post.nick_name");
        $create_time = date("Y-m-d H:i:s", time());
        $update_time = date("Y-m-d H:i:s", time());
        if($type == '2' ){
            $rule = [
                "police_station_id" => "require",
            ];
            $data = [
                "police_station_id" => $police_station_id
            ];
            $msg =[
                'police_station_id.require' => '请选择用户所在分局/派出所'
            ];
            $validate = new Validate($rule,$msg);
            if (!$validate->check($data)) {
                return $this->ajaxReturn(500,null,$validate->getError());
            }
        }

            $rule = [
                'account'=>  'require|max:20|alphaDash',
                'password' => 'require|length:6,20',
                'phone'  =>  'require|number|length:11',
                'sex' => 'require',
                "nick_name" => "require|max:20",
                "type" => "require",

            ];
            $data = [
                'account' => $account,
                'password' => $password,
                'phone' => $phone,
                'sex' => $sex,
                'type' => $sex,
                'nick_name' => $sex,

            ];
            $msg = [
                'account.require' => '用户名不能为空',
                'account.max' => '用户名不能超过20位',
                'account.alphaDash' => '用户名只能为字母数字下划线',
                'nick_name.require' => '昵称不能为空',
                'nick_name.max' => '昵称长度不能超过20位',
                'password.require' => '密码不能为空',
                'password.length' => '密码不能少于6位，超过20位',
                'phone.require' => '手机号码不能为空',
                'phone.length' => '请输入正确的手机号码',
                'phone.number' => '请输入正确的手机号码',
                'sex.require' => '性别不能为空',
                'type.require' => '请选择用户类型',

            ];
            $validate = new Validate($rule,$msg);
            if (!$validate->check($data)) {
                return $this->ajaxReturn(500,null,$validate->getError());
            }



        $model = new AppUserModel();
        $model->account = $account;
        $model->sex = $sex;
        $model->password = $password;
        $model->phone = $phone;
        $model->type = $type;
        $model->nick_name =$nick_name;
        $model->create_time = $create_time;
        $model->update_time = $update_time;
        $User = $model->where("account",$account)->find();
        if ($User){
            return $this->ajaxReturn(400,null,"该用户名已存在");
        }else{
            $model->save();
            if($type == '2'){
                $map['account']=$account;
                $map['type']=2;
                $id=$model->where($map)->find();
                $uid = $id['id'];
                $police_model = new UserPoliceStationModel();

                $police_model->uid=$uid;
                $police_model->police_station_id =$police_station_id;
                $police_model->save();
            }

            return $this->ajaxReturn(200,null,"注册成功");
        }


    }

    //获取省市区以及派出所
    public  function getRegion(){
        $type = input("post.type");
        $region = input("post.region");
        $province = input("post.province");
        $city = input('post.city');
        $model = new PoliceStationModel();
        $model->region=$region;
        if($type==2){
            $data=$model->distinct('province')
                ->field('province')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else if($province){
            $data=$model->where('province',$province)
                ->distinct('city')
                ->field('city')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else if($city){
            $data=$model->where('city',$city)
                ->distinct('region')
                ->field('region')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else{
            $data=$model->alias('a')
                ->where('region',$region)
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }
    }

    //更新
    public function update(){
        $account = input("post.account");
        $sex = input("post.sex");
        $password = input("post.password");
        $phone = input("post.phone");
        $nick_name = input("post.nick_name");
        $update_time = date("Y-m-d H:i:s", time());
        $rule = [
            'password' => 'require|length:6,20',
            'phone'  =>  'require|number|length:11',
            "nick_name"=>"require|max:20"

        ];
        $data = [
            'password' => $password,
            'phone' => $phone,
            'nick_name' => $nick_name,

        ];
        $msg = [
            'nick_name.require' => '用户名不能为空',
            'nick_name.max' => '用户名不能超过20位',
            'password.require' => '密码不能为空',
            'password.length' => '密码不能少于6位，超过20位',
            'phone.require' => '手机号码不能为空',
            'phone.length' => '请输入正确的手机号码',
            'phone.number' => '请输入正确的手机号码',
        ];
        $validate = new Validate($rule,$msg);
        if (!$validate->check($data)) {
            return $this->ajaxReturn(500,null,$validate->getError());
        }
        $model = new AppUserModel();
        $model->account = $account;
        $model->sex = $sex;
        $model->password = $password;
        $model->phone = $phone;
        $model->nick_name =$nick_name;
        $model->update_time = $update_time;
        $data = $model->where('account', $account)
            ->update(['password' => $password,'nick_name' => $nick_name,'phone' => $phone,'update_time'=>$update_time]);
        return $this->ajaxReturn(200,$data,'更新成功');
    }

    //删除
    public function del($account){
         $model = new AppUserModel();
         $data = $model->where('account', $account)->find();
         $uid =$data['id'];
         if($uid){
             $police_model = new UserPoliceStationModel();
             $police_model->where('uid',$uid)->delete();
         }
         $model->where('account', $account)->delete();
         return $this->ajaxReturn(200,null,'删除成功');
    }

    //详情
//    public function details($account){
//        $model = new AppUserModel();
//        $data = $model->alias('a')
//            ->where('account', $account)
//            ->join('think_user_room b','b.uid = a.id')
//            ->join('think_rooms c','c.id = b.room_id')
//            ->field('a.id,c.*')
//            ->select();
//        return $this->ajaxReturn(200,$data,'查询成功');
//    }


}